package org.pan.controller;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.pan.domain.PageResult;
import org.pan.domain.db.CardUser;
import org.pan.domain.enums.CardStatusEnum;
import org.pan.domain.enums.CardUserStateEnum;
import org.pan.service.CardUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 门禁前端控制器
 * Created by panmingzhi on 2015/12/24.
 */
@Controller
@RequestMapping("/accessControl")
public class AccessControlController{

    @Autowired
    private CardUserService cardUserService;

    @RequestMapping("/cardUserPageList")
    @ResponseBody
    public PageResult pageResult(@RequestParam("start") Integer start,@RequestParam("start") Integer limit){
        return cardUserService.createPageResult(start,limit,new CardUser());
    }

    @RequestMapping("/cardStatusList")
    @ResponseBody
    public ArrayNode cardStatusList(){
        return createFromEnumArr(CardStatusEnum.values());
    }

    @RequestMapping("/cardUserStatusList")
    @ResponseBody
    public ArrayNode cardUserStatusList(){
        return createFromEnumArr(CardUserStateEnum.values());
    }

    private ArrayNode createFromEnumArr(Object[] objArr){
        ObjectMapper objectMapper = new ObjectMapper();
        ArrayNode arrayNode = objectMapper.createArrayNode();
        for (Object obj : objArr) {
            ObjectNode objectNode = objectMapper.createObjectNode();
            objectNode.put("name",obj.toString());
            arrayNode.add(objectNode);
        }
        return arrayNode;
    }
}
